//
//  compareList.m
//  ShoppingCompare
//
//  Created by Wenjie on 11-5-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "CompareList.h"
#import "ItemList.h"
#import "Result.h"

@implementation CompareList

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [listItems count];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
	cell.backgroundColor = (indexPath.row % 2) ? [UIColor colorWithRed:0.88f green:0.93f blue:0.93f alpha:1.0f] : [UIColor colorWithRed:0.95f  green:0.97f blue:1.0f alpha:1.0f];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
	ItemList* aItem = [listItems objectAtIndex:indexPath.row];
	UITableViewCellStyle style = UITableViewCellStyleSubtitle;
	UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:@"BaseCell"];
	
	if (!cell)
		cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
	
	cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:aItem.imageURL]]];
	cell.textLabel.text = aItem.title;
	
	[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15]];
	cell.textLabel.numberOfLines = 3;
	cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", aItem.nick, aItem.price];
	
	cell.accessoryType = (aItem.selected) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
		
	cell.textLabel.backgroundColor = cell.imageView.backgroundColor = cell.detailTextLabel.backgroundColor = [UIColor clearColor];
	return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 80.0f;
}

- (id)init {
	if(self = [super init]) {		
		self.title = @"Compare List";
		self.navigationItem.rightBarButtonItem = self.editButtonItem;
	}
	return self;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
	[listItems removeObjectAtIndex:indexPath.row];
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	[navi popViewControllerAnimated:NO];
	[navi popViewControllerAnimated:NO];
	Result* result = [[Result alloc]init];
	[navi pushViewController:result animated:NO];
	CompareList* compare = [[CompareList alloc]init];
	[navi pushViewController:compare animated:YES];
}

- (void) showAction {
	UIActionSheet* menu = [[UIActionSheet alloc] initWithTitle:@"Select Action" delegate:self 
											 cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
											 otherButtonTitles:@"remove selected items", @"remove all items", nil];
	[menu showInView:self.view];
}

- (void) actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger) buttonIndex {
	[actionSheet release];	
	
	switch (buttonIndex) {
		case 0:
			for (int i = [listItems count] - 1; i >= 0; i--)  {
				ItemList* aItem = [listItems objectAtIndex:i];
				if (aItem.selected) {
					[listItems removeObjectAtIndex:i];
				}

			}
			
			break;
		case 1:
			[listItems removeAllObjects];
			break;
		default:
			break;
			
	}
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	[navi popViewControllerAnimated:NO];
	[navi popViewControllerAnimated:NO];
	Result* result = [[Result alloc]init];
	[navi pushViewController:result animated:NO];
	CompareList* compare = [[CompareList alloc]init];
	[navi pushViewController:compare animated:YES];

}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end